//
//  UIImage+AsyncDraw.m
//  SinaWeiBo
//
//  Created by WGP on 16/4/28.
//  Copyright © 2016年 WGP. All rights reserved.
//

#import "UIImage+AsyncDraw.h"

@implementation UIImage (AsyncDraw)

-(void)WP_AsyncDrawImageWith:(CGSize)size bgColor:(UIColor *)bgColor isCorner:(BOOL)isCorner finished:(void(^)(UIImage *image))finished
{
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
       
        CGRect rect = CGRectMake(0, 0, size.width, size.height);
        UIGraphicsBeginImageContextWithOptions(size, bgColor != nil, [UIScreen mainScreen].scale);
        
//        设置背景颜色
        [bgColor setFill];
        UIRectFill(rect);
        
        if (isCorner) {
            
           UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:rect];
//            让后面绘制的元素在路径里面
            [path addClip];
        }
//        绘制
        [self drawInRect:rect];
        
//        获得图片
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        
//        关闭徒刑上下文
        UIGraphicsEndImageContext();
        
//        主线程返回绘制的图片
        dispatch_async(dispatch_get_main_queue(), ^{
           
            finished(newImage);
            
        });
    });
}

@end







